package com.todo.admin.service;

import com.todo.common.utils.StringUtils;

import com.todo.component.third.party.oss.AliyunOssUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author azhebuxing
 * @date 2024/12/2 20:38
 * @description
 */
@Slf4j
@Service
public class FileService {

    @Resource
    private AliyunOssUtils aliyunOssUtils;

    public String upload(MultipartFile file) {
        return aliyunOssUtils.upload(file, "default");
    }

    public List<String> multipartUpload(List<MultipartFile> fileList) {
        return multipartUpload(fileList, "default");
    }


    public String upload(MultipartFile file, String bizCode) {
        // 阿里存储
        String filePath = aliyunOssUtils.upload(file, bizCode);
        // 存 储 记 录
        log.info("FileService#upload 上传文件完成：{}", filePath);
        return filePath;
    }

    public List<String> multipartUpload(List<MultipartFile> fileList, String bizCode) {
        if (fileList.isEmpty()) {
            return Collections.emptyList();
        }
        List<String> urlList = new ArrayList<>();
        for (MultipartFile file : fileList) {
            try {
                String uploadUrl = upload(file, bizCode);
                if (!StringUtils.isBlank(uploadUrl)) {
                    urlList.add(uploadUrl);
                }
            } catch (Exception e) {
                log.error("FileService#multipartUpload 上传文件失败", e);
            }
        }
        log.info("FileService#multipartUpload 上传文件完成：{}", urlList);
        return urlList;
    }
}
